<template>
	<view>
		<scroll-view scroll-y="true">
			<view class="search">
				<!-- input 监听输入状态 confirm回车触发事件 -->
				<uni-search-bar v-model="keyword" @confirm="confirm" @input="input"
					placeholder="想听什么歌呢？" />
			</view>
			<template v-if="searchingStatus ==='default'">
				<view class="hot">
					<view class="title">热搜榜</view>
					<view class="item" v-for="(item,index) in searchHot" :key="index"
						@tap="handleToWord(item.searchWord)">
						<view class="top">{{ index + 1 }}</view>
						<view class="word">
							<view>
								{{ item.searchWord }}
								<image :src="item.iconType ? item.iconUrl : ''" mode="aspectFit"></image>
							</view>
							<view>{{ item.content }}</view>
						</view>
						<text class="count">{{ item.score }}</text>
					</view>
				</view>
			</template>

			<template v-else-if="searchingStatus ==='result'">
				<view class="hot">
					<view class="title">搜索结果</view>
					<view class="item" v-for="(item,index) in searchList" :key="index" @tap="navPlayer(item.id)">
						<view class="top">{{ index + 1 }}</view>
						<view class="word">
							<view>
								{{ item.name}} - 专辑: {{item.album.name}}
								<image :src="item.iconType ? item.iconUrl : ''" mode="aspectFit"></image>
							</view>
							<view>歌手-{{ item.artists[0].name}}</view>
						</view>
						<text class="count">{{ item.duration }}</text>
					</view>
				</view>
			</template>

			<template v-else-if="keyword ===''">
				<view class="hot">
					<view class="searchTop">
						<view class="title">最近搜索</view>
						<image @tap="historydel" class="delete" src="../../static/images/delete.png"></image>
					</view>
					<view class="bj">
						<view v-for="(item,index) in historyList" :key="index">
							<view class="zhuijin" @tap="choose(item)">{{item}}</view>
						</view>
					</view>
				</view>
			</template>

			<template v-else-if="searchingStatus ==='searching'">
				<view class="hot">
					<view class="searchTop">
						<view class="title">搜索建议</view>
					</view>
					<view class="list_item">
						<view class="jyi" v-for="(item,index) in suggestWord" :key="index">
							<view class="list_index">{{index+1}}</view>
							<view class="Song" @tap="choose(item)">{{item}}</view>
						</view>
					</view>
				</view>
			</template>
		</scroll-view>
	</view>
</template>

<script>
	import {
		searchHot,
		searchWord,
		searchSuggest
	} from '../../common/api.js'
	export default {
		data() {
			return {
				title: '搜索页',
				keyword: '',
				searchingStatus: 'default',
				searchHot: [],
				searchList: [],
				suggestWord: [],
				historyList: []
			}
		},
		onLoad() {
			searchHot().then((res) => {
				console.log("searchHot", res);
				if (res[1].data.code == '200') {
					this.searchHot = res[1].data.data;
				}
			});

			// getStorage()是获取数据事件，用到uni.getStorage可以没有data,但是key和success一定要有。

			uni.getStorage({
				key: 'searchHistory',
				success: (res) => {
					console.log("searchHistory", res)
					this.historyList = res.data;
					console.log("historyList1", this.historyList)
				}
			});
		},

		methods: {
			historydel() {
				localStorage.removeItem('searchHistory')
				this.historyList = ''

				// uni.showModal({
				// 	title: '提示',
				// 	content: '你确定要删除吗',
				// 	success(res) {
				// 		console.log("res..",res)
				// 		if (res.confirm) {
				// 			console.log('确定');

				// 		} else if (res.cancel) {
				// 			console.log('取消');
				// 		}
				// 	}
				// });

			},

			//点击事件
			handleToWord(searchWord) {
				console.log("点击searchWord", searchWord)
				this.confirm()
				this.keyword = searchWord;
				
			},
			choose(index) {
				this.confirm()
				this.keyword = index;
			},

			navPlayer(id) {
				console.log(id)
				uni.navigateTo({
					url: '/components/index/player?songId=' + id
				})
			},

			// 搜索框
			input() {
				this.searchingStatus = 'searching'
				let word = this.keyword
				var arr = []
				searchSuggest(word).then(res => {
					console.log("searchSuggest", res)
					arr = res[1].data.result.allMatch.map((item, index) => {
						return item.keyword
					})
					this.suggestWord = arr
					console.log("搜索框suggestWord", this.suggestWord)
				})
			},


			confirm() {
				this.searchingStatus = 'result'
				searchWord(this.keyword).then((res) => {
					console.log("searchWord", res)
					if (res[1].data.code == '200') {
						this.searchList = res[1].data.result.songs;
					}
					console.log("searchList", this.searchList)
					this.historyList.unshift(this.keyword);
					this.historyList = [...new Set(this.historyList)];
					if (this.historyList.length > 10) {
						this.historyList.length = 10;
					}

					// setStorage()是存储数据事件，用到uni.setStorage，key和data都得有
					uni.setStorage({
						key: 'searchHistory',
						data: this.historyList
					})
					console.log("historyList2", this.historyList)
				})
			}
		}
	}
</script>

<style>
	.search {
		background: #f7f7f7;
		border-radius: 40rpx;
		align-items: center;
	}

	.search text {
		/* margin: 0 27rpx; */
	}

	.search input {
		font-size: 26rpx;
		flex: 1;
	}

	.hot {
		margin: 30rpx 30rpx;
		font-size: 26rpx;
		color: #bebebe;
	}

	.hot .title {}

	.hot .item {
		display: flex;
		align-items: center;
		margin-top: 40rpx;
	}

	.hot .top {
		width: 60rpx;
		color: #fb2221;
		font-size: 34rpx;
	}

	.hot .word {
		flex: 1;
	}

	.hot .word view:nth-child(1) {
		font-size: 36rpx;
		color: black;
	}

	.hot .word image {
		width: 48rpx;
		height: 22rpx;
	}

	.bj {
		/* display: flex; */
		justify-content: space-around;
		margin-top: 10rpx;
	}

	.zhuijin {
		color: #909090;
		float: left;
		margin-left: 15rpx;
		margin-top: 7rpx;
	}

	.delete {
		width: 30rpx;
		height: 30rpx;
		position: absolute;
		right: 0;
	}

	.searchTop {
		display: flex;
		position: relative;
	}

	.list_item {
		color: #888;
		font-size: 42rpx;
		margin-top: 24rpx;
		line-height: 92rpx;
	}
	.list_index{
		color: #fc2221;
	}
	.jyi{
		display: flex;
	}
	.Song{
		margin-left: 30rpx;
	}
</style>
